package org.example.springcloud.controller;

import com.netflix.discovery.EurekaClient;
import org.example.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${user.url}")
    private String url;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/order/{id}")
    public User getUser(@PathVariable long id) {
        User user = restTemplate.getForObject(
                "http://PROVIDER-USER/user/" + id,
                User.class
        );
        ;
        return user;
    }

    @GetMapping("/test")
    public String test() {
        ServiceInstance choose = loadBalancerClient.choose("PROVIDER-USER");
        System.out.println("1111" + choose.getServiceId() + choose.getHost()
                + choose.getPort());
        return "1";
    }
}
